﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 实验一._2//对数组的相关操作
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] ar = { 2, 5, 3, 7 };
            array a = new array(ar);
            a.output();
            a.insert(2,2);
            
            a.output();
            a.insert(6, 2);
            a.output();
            a.insert(3,3);
            a.output();
            a.search(9);
            a.search(6);

        }
    }
    class array
    {
        public array(int[] array)//构造函数
        {
           
            Array = array;
            
        }
        public void output()//输出打印
        {
            Console.WriteLine("打印数组：");
            foreach (int i in Array)
                Console.WriteLine($"{i}\t");
           
        }
        public void insert(int locate, int value)//插入功能
        {
            /*int Locate;//插入位置
            int value;//插入值
            bool k = true;
            while (k)
            {
                /*Console.WriteLine("输入插入的数字:");
                string s = Console.ReadLine();
                value = int.Parse(s);
                Console.WriteLine("输入要插入的位置：");
                s = Console.ReadLine();
                locate = int.Parse(s);
                if (locate > Array.Length)
                    Console.WriteLine("参数错误！");
                else
                {
                    int[] temp;//用来复制数组Array，并完成插入
                    temp = new int[Array.Length + 1];
                    int i=0,j=0;
                    int index = 1;
                    while (index < locate)
                    {
                        temp[i++] = Array[j++];
                        index++;
                    }
                    temp[i++] = value;
                    while (j != Array.Length)
                    {
                        temp[i++] = Array[j++];
                    }
                    Array = temp;//将插入完成的数组temp重新复制给Array
                    Console.WriteLine("操作成功！");
                    k = false;
                }
            }*/
            if (locate > Array.Length)
                Console.WriteLine("参数错误！");
            else
            {
                int[] temp;//用来复制数组Array，并完成插入
                temp = new int[Array.Length + 1];
                int i = 0, j = 0;
                int index = 1;
                while (index < locate)
                {
                    temp[i++] = Array[j++];
                    index++;
                }
                temp[i++] = value;
                while (j != Array.Length)
                {
                    temp[i++] = Array[j++];
                }
                Array = temp;//将插入完成的数组temp重新复制给Array
                Console.WriteLine("操作成功！");
            }
        } 
        public void search(int value)//查找
        {
            //int value;
            bool k = true;
            /* while (k)
             {
                 int i;
                 int index=0;
                Console.WriteLine("输入要查找的的值:");
                 string s = Console.ReadLine();
                 value = int.Parse(s);
                 for (i = 0; i < Array.Length; i++)
                 {
                     if (Array[i] == value)
                     {
                         index = i;
                         k = false;
                         break;
                     }

                 }
                 if(k==true)
                     Console.WriteLine($"没找到值{value}！");
                 else
                     Console.WriteLine($"值{value}的下标为：{index}");
             }*/
            int i;
            int index = 0;
            for (i = 0; i < Array.Length; i++)
            {
                if (Array[i] == value)
                {
                    index = i;
                    k = false;
                    break;
                }

            }
            if (k == true)
                Console.WriteLine($"没找到值{value}！");
            else
                Console.WriteLine($"值{value}的下标为：{index}");
        
        }
        int[] Array;
        

    }
}
